load("methods.Object.js");
load("methods.Array.js");
load("JSON.schema.reg.js");
load("JSON.path.js");
load("JSON.ref.js");
load("JSON.schema.err.js");
load("JSON.schema.err.messages.en.js");
load("JSON.schema.ext.walker.js");
load("JSON.schema.ext.js");

function loadSchemaFromFile(file) {
	var schema, stream = new Stream(file, "r");
	eval("schema = " + stream.readFile() + ";");
	return schema;
}

(function main() {
	if (arguments.length < 1) {
		writeln("You must provide at least one argument. This argument should be the name of the file containing the schema you wish to validate.");
		return;
	}

	var stream, schema, retVal;

	for (var i=1; i < arguments.length; i++) {
		JSON.schema.registry.register(loadSchemaFromFile(arguments[i]));
	}

	retVal = JSON.schema.ext.validate(loadSchemaFromFile(arguments[0]));

	if (retVal.valid) {
		writeln("\nNo errors found!");
	} else {
		var out = "\n";
		for (var i=0; i < retVal.errors.length; i++) {
			out += 
				(i+1) + ". " + 
				retVal.errors[i].code + ": " + 
				JSON.schema.err.localize('en', retVal.errors[i]) + "\n";
		}

		writeln(out);
	}
}.apply(null, system.arguments));